
import Foundation
import UIKit

//extension UITableView: LXNameSpaceWrappable {}

public extension LXNameSpaceWrapper where Base: UITableView {
    func registerCellClass(_ aClass: UITableViewCell.Type) {
        let name = String(describing: aClass)
        base.register(aClass, forCellReuseIdentifier: name)
    }
    func registerCellNib(_ aClass: UITableViewCell.Type) {
        let name = String(describing: aClass)
        let nib = UINib(nibName: name, bundle: nil)
        base.register(nib, forCellReuseIdentifier: name)
    }
    
    func dequeueReusableCell<T: UITableViewCell>(_ aClass: T.Type) -> T! {
        let name = String(describing: aClass)
        guard let cell = base.dequeueReusableCell(withIdentifier: name) as? T else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
    func dequeueReusableCell<T: UITableViewCell>(_ aClass: T.Type, indexPath: IndexPath) -> T! {
        let name = String(describing: aClass)
        guard let cell = base.dequeueReusableCell(withIdentifier: name, for: indexPath) as? T else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
    
    
    func registerHeaderFooterNib<T: UIView>(_ aClass: T.Type) {
        let name = String(describing: aClass)
        let nib = UINib(nibName: name, bundle: nil)
        base.register(nib, forHeaderFooterViewReuseIdentifier: name)
    }
    
    func registerHeaderFooterClass<T: UIView>(_ aClass: T.Type) {
        let name = String(describing: aClass)
        base.register(aClass, forHeaderFooterViewReuseIdentifier: name)
    }
    
    func dequeueReusableHeaderFooter<T: UIView>(_ aClass: T.Type) -> T! {
        let name = String(describing: aClass)
        guard let cell = base.dequeueReusableHeaderFooterView(withIdentifier: name) as? T else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
    
}

@objc public extension UITableView {
    @objc func lx_registerCellClass(_ aClass: UITableViewCell.Type) {
        let name = String(describing: aClass)
        self.register(aClass, forCellReuseIdentifier: name)
    }
    @objc func lx_registerCellNib(_ aClass: UITableViewCell.Type) {
        let name = String(describing: aClass)
        let nib = UINib(nibName: name, bundle: nil)
        self.register(nib, forCellReuseIdentifier: name)
    }
    
    @objc func lx_dequeueReusableCell(_ aClass: UITableViewCell.Type) -> UITableViewCell {
        let name = String(describing: aClass)
        guard let cell = self.dequeueReusableCell(withIdentifier: name) else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
    @objc func lx_dequeueReusableCell(_ aClass: UITableViewCell.Type, indexPath: IndexPath) -> UITableViewCell {
        let name = String(describing: aClass)
        return self.dequeueReusableCell(withIdentifier: name, for: indexPath)
    }
    
    
    @objc func lx_registerHeaderFooterNib(_ aClass: UITableViewHeaderFooterView.Type) {
        let name = String(describing: aClass)
        let nib = UINib(nibName: name, bundle: nil)
        self.register(nib, forHeaderFooterViewReuseIdentifier: name)
    }
    
    @objc func lx_registerHeaderFooterClass(_ aClass: UITableViewHeaderFooterView.Type) {
        let name = String(describing: aClass)
        self.register(aClass, forHeaderFooterViewReuseIdentifier: name)
    }
    
    @objc func lx_dequeueReusableHeaderFooter(_ aClass: UITableViewHeaderFooterView.Type) -> UITableViewHeaderFooterView {
        let name = String(describing: aClass)
        guard let cell = self.dequeueReusableHeaderFooterView(withIdentifier: name) else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
}
